﻿// ---------------------------------------------------------------------
// Partial của lớp SSupervisor
//
// Nội dung:
//    + Các hàm xử lý lúc bắt đầu game
//
// Copyright (c) 2009 YugiOH
//
// Nguyễn Hữu Lê Trọng Tín      YM: ilu2009             ĐT: 0906520611 
// Võ Phước Hậu                 YM: phuochauspkt        ĐT: 0979202707
// ---------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BYServer;

namespace ServerYuGi
{
    public partial class SSupervisor
    {

        int demPlayerBegin = 0;

        #region ONLINE
        //void Process_PLAYER_LOGIN(params object[] param)
        //{           
        //    Players[demPlayerBegin].ID = (int)param[0];            
        //    Console.WriteLine("Player login: ID = " + Players[demPlayerBegin].ID);
        //    Players[demPlayerBegin].SoBaiChuaChia.Clear();
        //    Console.WriteLine("Start Get Card");
            
        //    List<int> cards = yugiService.GetAllCard(Players[demPlayerBegin].ID).Deck;  // use for online            
            
        //    Console.WriteLine("Card of Player {0} : Count = {1}", Players[demPlayerBegin].ID, cards.Count);
        //    if(cards.Count==0)
        //    {
        //        cards = new List<int>();
        //        for(int i=0;i<40;i++)
        //            cards.Add(random.Next(78));
        //    }
        //    foreach (int i in cards)
        //    {
        //        Players[demPlayerBegin].AddCardToBoBaiChuaChia(i);
        //    }
        //    Players[demPlayerBegin].ID = demPlayerBegin;
        //    demPlayerBegin++;           
        //    Console.WriteLine("So luong player Begin: " + demPlayerBegin.ToString());
        //    if(demPlayerBegin == 2)
        //        CheckBatDauVanMoi();
        //}
        #endregion

        void Process_PLAYER_LOGIN(params object[] param)
        {
            Players[demPlayerBegin].ID = (int)param[0];
            Console.WriteLine("Player login: ID = " + Players[demPlayerBegin].ID);
            Players[demPlayerBegin].SoBaiChuaChia.Clear();
            Console.WriteLine("Start Get Card");         
            List<int> cards = new List<int>(); // use for offline
            Console.WriteLine("Card of Player {0} : Count = {1}", Players[demPlayerBegin].ID, cards.Count);
            if (cards.Count == 0)
            {
                cards = new List<int>();
                for (int i = 0; i < 78; i++)
                    cards.Add(i);
            }
            foreach (int i in cards)
            {
                Players[demPlayerBegin].AddCardToBoBaiChuaChia(i);
            }
            Players[demPlayerBegin].ID = demPlayerBegin;
            Players[demPlayerBegin].trueID = (int)param[0];
            demPlayerBegin++;
            Console.WriteLine("So luong player Begin: " + demPlayerBegin.ToString());

            if (demPlayerBegin == 2)
            //    CheckBatDauVanMoi();
            {
                BatDauVan1();
                Table.SendMessageAll(Common.YMSG.RPS_CanClick);
            }
        }


        void Process_RPS_Hand(params object[] param)
        {
            
            for (int i = 0; i < Players.Count; i++)
            {
                if (((int)param[0] ) ==Players[i].trueID)
                    continue;
                Table.SendMessageToID(Players[i].ID, Common.YMSG.RPS_Hand, param[0],param[1]);
            }          
        }

        void Process_RPS_Chose(params object[] param)
        {
            for (int i = 0; i < Players.Count; i++)
            {
                if ((int)param[0] == Players[i].trueID)
                {
                    if ((int)param[1] == 1)
                        TurnManager.IDPlayerTrongLuot = Players[i].ID;
                    continue;
                }
                if ((int)param[1] == 2)
                    TurnManager.IDPlayerTrongLuot = Players[i].ID;
                Table.SendMessageToID(Players[i].ID, Common.YMSG.RPS_Chose, param[0], param[1]);
            }      
        }
        void Process_RPS_Finish(params object[] param)
        {
            CheckBatDauVanMoi();
        }


        void CheckBatDauVanMoi()
        {
            if (Players.Count == 2)
            {
                Console.WriteLine("Bat Dau Van");
                BatDauVan();
            }
        }
    }
}
